package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.dataModel.DataModelDTO;
import com.etone.smartAudit.service.DataModelService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.dataModel.DataModelDetailVO;
import com.etone.smartAudit.vo.dataModel.DataModelListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author: sovy
 * @date: 2020/11/10
 * @description:
 */
@AllArgsConstructor
@RestController
@Api(tags = "数据模型管理", value = "DataModelController")
public class DataModelController {

    private DataModelService dataModelService;

    @ApiOperation(value = "获取所有公共数据模型《完》")
    @GetMapping("/dataModels/public")
    public PagerVO<DataModelListVO> listPublic(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型名称（英文名称和中文名称）") @RequestParam(required = false) String modelName,
            @ApiParam(value = "三级分类id") @RequestParam(required = false) List<Integer> level3Ids,
            @ApiParam(value = "数据库名称") @RequestParam(required = false) String databaseName,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "被授权项目id") @RequestParam(required = false) Integer passiveItemId,
            @ApiParam(value = "是否授权") @RequestParam(required = false) Boolean isAuth,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return dataModelService.listPublic(admin, modelName, level3Ids, databaseName, itemId, passiveItemId, isAuth, page, pageSize);
    }

    @ApiOperation(value = "获取所有私有数据模型《完》")
    @GetMapping("/dataModels/private")
    public PagerVO<DataModelListVO> listPrivate(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "模型名称（英文名称和中文名称）") @RequestParam(required = false) String modelName,
            @ApiParam(value = "三级分类id") @RequestParam(required = false) List<Integer> level3Ids,
            @ApiParam(value = "数据库名称") @RequestParam(required = false) String databaseName,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return dataModelService.listPrivate(admin, modelName, level3Ids, databaseName, itemId, page, pageSize);
    }

    @ApiOperation(value = "获取数据模型详情《完》")
    @GetMapping("/dataModels/{id}")
    public DataModelDetailVO detail(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return dataModelService.detail(id, admin);
    }

    @ApiOperation(value = "增加数据模型《完》")
    @PostMapping("/dataModels")
    public void add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "dataModelDTO", required = true) @RequestBody @Validated DataModelDTO dataModelDTO
    ) {
        dataModelService.save(null, admin, dataModelDTO);
    }

    @ApiOperation(value = "编辑数据模型《完》")
    @PutMapping("/dataModels/{id}")
    public void edit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "dataModelDTO", required = true) @RequestBody @Validated DataModelDTO dataModelDTO
    ) {
        dataModelService.save(id, admin, dataModelDTO);
    }

    @ApiOperation(value = "模型共享《完》")
    @PutMapping("/dataModels/{ids}/toPublic")
    public void toPublic(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "ids", required = true) @PathVariable List<Integer> ids
    ) {
        dataModelService.toPublic(ids);
    }

    @ApiOperation(value = "查询英文表名是否存在《未完》", notes = "同一个系统同一个库下的英文表名不可重复")
    @GetMapping("/dataModels/enTableName/exists")
    public Boolean checkEnTableNameExists(
            @ApiParam(value = "元数据id(编辑时判断是否是自身)") @RequestParam(required = false) Integer id,
            @ApiParam(value = "项目id", required = true) @RequestParam Integer itemId,
            @ApiParam(value = "英文表名", required = true) @RequestParam String enTableName
    ) {
        return false;
    }

    @ApiOperation(value = "删除数据模型《完》")
    @DeleteMapping("/dataModels/{id}")
    public void delete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        dataModelService.delete(id, admin);
    }

    @ApiOperation(value = "删除数据模型《完》")
    @DeleteMapping("/dataModels/batch/{ids}")
    public void batchDelete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "ids", required = true) @PathVariable List<Integer> ids
    ) {
        dataModelService.batchDelete(ids, admin);
    }
}
